<template>
  <div class="table-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">删除</el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>
        <el-form ref="form" :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model="queryForm.name" placeholder="标题" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" native-type="submit" type="primary" @click="handleQuery">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
      @selection-change="setSelectRows"
      @sort-change="tableSortChange"
    >
      <el-table-column show-overflow-tooltip type="selection" width="55" />
      <el-table-column label="序号" show-overflow-tooltip width="95">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="SID" prop="sid" show-overflow-tooltip />
      <el-table-column label="名称" prop="name" show-overflow-tooltip />
      <el-table-column label="状态" show-overflow-tooltip>
        <template #default="{ row }">
          <el-tooltip class="item" :content="row.state" effect="dark" placement="top-start">
            <el-tag :type="row.state | statusFilter">{{ getState(row.state) }}</el-tag>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="标签" prop="tag" show-overflow-tooltip sortable />
      <el-table-column label="描述" prop="devicesInfo" show-overflow-tooltip />
      <el-table-column label="时间" prop="createtime" show-overflow-tooltip width="200" />
      <el-table-column label="操作" show-overflow-tooltip width="180px">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <table-edit ref="edit" />
  </div>
</template>

<script>
  import { doDelete, getList } from '@/api/table'
  import TableEdit from './components/TableEdit'
  import api_device from '@/api/device/API_device.js'

  export default {
    name: 'ComprehensiveTable',
    components: {
      TableEdit,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        imgShow: true,
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    beforeDestroy() {},
    mounted() {},
    methods: {
      tableSortChange() {
        const imageList = []
        this.$refs.tableSort.tableData.forEach((item, index) => {
          imageList.push(item.img)
        })
        this.imageList = imageList
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleAdd() {
        this.$refs['edit'].showEdit()
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleDelete(row) {
        if (row.sid) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            await this.doDelete(row.sid)
            this.$baseMessage('删除成功', 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              // const { msg } = await doDelete({ ids: ids })
              this.selectRows.forEach(async (item, index) => {
                await this.doDelete(item.sid)
              })
              this.$baseMessage('删除成功', 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        let dataQ = {
          name: this.queryForm.name,
        }
        const { datas, resp_code, resp_msg } = await api_device.query(dataQ)
        if (resp_code < 0) {
          this.$baseMessage('查询错误', 'error')
        } else {
          if (datas.content && datas.content.length > 0) {
            console.info('数据', datas)
            this.list = datas.content
            this.total = datas.totalSize
          } else {
            this.$baseMessage('数据为空', 'error')
          }
        }

        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
      async doDelete(sid) {
        let when = {
          sid: sid,
        }
        const { datas, resp_code, resp_msg } = await api_device.delete(when)
        if (resp_code < 0) {
          this.$baseMessage(resp_msg, 'error')
        }
      },
      getState(state) {
        if (state == 1) {
          return '在线'
        } else if (state == 0) {
          return '离线'
        }
        return '未知'
      },
    },
  }
</script>
